//
//  RegisterViewController.m
//  BaChat
//
//  Created by 刘洋 on 16/6/14.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "RegisterViewController.h"
#import "LabelAndTextView.h"

@interface RegisterViewController ()

@property(nonatomic,retain)LabelAndTextView *nameView;
@property(nonatomic,retain)LabelAndTextView *passwordView;
@property(nonatomic,retain)LabelAndTextView *passwordSureView;
@property(nonatomic,retain)BaseButton *registerButton;


@end

@implementation RegisterViewController


-(void)dealloc{
 //   Block_release(loginBlock);
    [_nameView release];
    [_passwordView release];
    [_passwordSureView release];
    [super dealloc];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.title = @"注册";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

      self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navigationbarnew_light"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    self.nameView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(ROOTWIDTH / 15, ROOTHEIGHT / 8, ROOTWIDTH / 4 * 3 + 30, 40)];
    self.nameView.mylabel.text = @"帐号";
    self.nameView.myTextField.placeholder = @"    用户名/手机号";
    [self.view addSubview:self.nameView];
    [_nameView release];

    
    
    self.passwordView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(self.nameView.frame.origin.x, self.nameView.frame.origin.y + self.nameView.frame.size.height + 20, self.nameView.frame.size.width, self.nameView.frame.size.height)];
    self.passwordView.mylabel.text = @"密码";
    self.passwordView.myTextField.placeholder = @"    请填写密码";
    self.passwordView.myTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordView];
    [_passwordView release];

    
    
    
    
    self.passwordSureView = [[LabelAndTextView alloc]initWithFrame:CGRectMake(self.passwordView.frame.origin.x, self.passwordView.frame.origin.y + self.passwordView.frame.size.height + 20, self.passwordView.frame.size.width, self.passwordView.frame.size.height)];
    self.passwordSureView.mylabel.text = @"确认密码";
    self.passwordSureView.myTextField.placeholder = @"    确认密码";
    self.passwordSureView.myTextField.secureTextEntry = YES;
    [self.view addSubview:self.passwordSureView];
    [_passwordSureView release];

    
    
    
    
    BaseButton *registerButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(30, self.passwordSureView.frame.origin.y + self.passwordSureView.frame.size.height + ROOTHEIGHT / 15, ROOTWIDTH - 60, 40);

    [self.view addSubview:registerButton];
    
    
    registerButton.backgroundColor = RGBAColor(205, 201, 201);
    [registerButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    registerButton.layer.masksToBounds = YES;
    registerButton.layer.cornerRadius = 5;
    
    
    //取消图片Button的点击效果
    registerButton.adjustsImageWhenHighlighted = NO;
    [registerButton addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];

    self.registerButton = registerButton;
    
   
//监听输入内容的情况
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonSelected:) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    // Do any additional setup after loading the view.
}


-(void)leftAction{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    
}




#pragma mark -- textFiel改变时通知button改变样式
-(void)buttonSelected:(NSNotification *)noti{

    NSString *name = self.nameView.myTextField.text;
    NSString *password = self.passwordView.myTextField.text;
    NSString *passwordSure = self.passwordSureView.myTextField.text;

//任意一个为空,注册按钮都是暗的没有点击效果
    if (name.length == 0 || password.length == 0 || passwordSure.length == 0) {
        
     self.registerButton.userInteractionEnabled = NO;
        self.registerButton.userInteractionEnabled = NO;
        self.registerButton.backgroundColor = RGBAColor(205, 201, 201);
        [self.registerButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//都不为空,注册按钮变亮可以注册
    }else{
        self.registerButton.userInteractionEnabled = YES;
        self.registerButton.backgroundColor = RGBAColor(138, 43, 226);
        [self.registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  
    }
    
    
    
    
}





#pragma mark -- 注册按钮
-(void)registerButton:(BaseButton *)button{
    
    NSString *name = self.nameView.myTextField.text;
    NSString *password = self.passwordView.myTextField.text;
    NSString *passwordSure = self.passwordSureView.myTextField.text;

//如果两个次数的密码不同,提示密码不同
        if (![password isEqualToString:passwordSure]) {
            
            [self insertMBProgressHUDWithMessage:@"两次密码输入不同"];

//否则密码相同,可以继续注册
        }else{
            
            //注册账号
            [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:name password:passwordSure withCompletion:^(NSString *username, NSString *password, EMError *error) {
                
                if (!error) {
                    NSLog(@"注册成功");
                    self.passValue(username,password);
                    NSLog(@"%@ %@",username,password);
                    [[NSNotificationCenter defaultCenter]postNotificationName:UITextFieldTextDidChangeNotification object:nil];
                    [self dismissViewControllerAnimated:YES completion:nil];

                    
                }else{
//注册失败,原因只能用一个,就是账户存在
                    [self insertMBProgressHUDWithMessage:@"该帐户已存在"];
                    
                }
       
                
            } onQueue:dispatch_get_main_queue()];
       
        }
        


}




-(void)insertMBProgressHUDWithMessage:(NSString *)message{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.labelText = message;
    [hud hide:YES afterDelay:1];
    
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.nameView.myTextField resignFirstResponder];
    [self.passwordView.myTextField resignFirstResponder];
    [self.passwordSureView.myTextField resignFirstResponder];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
